package com.service.cm;


import com.bpms.common.CommonUtil;
import com.bpms.common.JdbcWorker;
import com.bpms.common.VO;
import com.bpms.common.VOList;
import com.bpms.esb.IService;

public class Getmenutree implements IService{
	/**
	 * 输入
	 * 无
	 * 输出
	 * result 结果0000成功，9999失败
	 * msg 描述信息
	 * title     标题
	 * titleColName 标题字段名称
	 * parentIdColName  父节点字段名称
	 * columns 字段标识
	 * types 字段类型
	 * list -VOList
	 */
	public VO exec(VO arg0) {
		// TODO Auto-generated method stub
		JdbcWorker j = null;
		VO vo=null;
		try {
			j = new JdbcWorker();
			String sql="select * from bpm_menutree";
			String type=arg0.getAttr("menutype");
			if(!"".equals(type)){
				sql+=" where menutype='"+type+"'";
			}
			VOList vol=j.readAll(new VO(), sql);
			
			vo=getTreeModel(vol);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			CommonUtil.debug("getmenutree exception", e);
			vo.setAttr("result", "9999");
			vo.setAttr("msg", "6020");
		}
		vo.setAttr("titleColName", "TITLE");
		vo.setAttr("parentIdColName", "parent_id");
		vo.setAttr("stackTitleName", "console");
		vo.setAttr("columns", "TITLE,SRC,ORDERNUM");
		vo.setAttr("types", "TEXT,TEXT,TEXT");
		vo.setAttr("result", "0000");
		return vo;
	}
	
	public VO getTreeModel(VOList vol){
		VO vo=null;
		for(int i=0;i<vol.listData.size();i++){
			VO temp=(VO)vol.listData.get(i);
			if("-1".equals(temp.getAttr("parent_id"))){
				vo=temp;
				break;
			}
		}
		return buildTree(vo,vol);
		
	}
	
	@SuppressWarnings("unchecked")
	public VO buildTree(VO v,VOList vol){
		
		VOList vect=new VOList();
		for(int i=0;i<vol.listData.size();i++){
			VO temp=(VO)vol.listData.get(i);
			if(v.getAttr("autoid").equals(temp.getAttr("parent_id"))){
				vect.listData.add(temp);
				buildTree(temp,vol);
			}
			v.setObj("list",vect);
		}
		return v;
	}
	

}
